﻿package
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.events.*;
	
	public class Host extends MovieClip {
		public var hostId:uint;
		public var objectHighlight:Shape;
		
		public function Host() {
			// generate a random id for every instance
			do {
				hostId = Math.round(Math.random() * Math.random() * 13000000);
			} while (hostId == 0);
			//trace(hostId);

			addEventListener(MouseEvent.MOUSE_DOWN, startMove);
			addEventListener(MouseEvent.MOUSE_UP, stopMove);
			addEventListener(MouseEvent.CLICK, clickDrawLine);
			addEventListener(MouseEvent.MOUSE_MOVE, updateLines);
			addEventListener(MouseEvent.MOUSE_OVER, highlightObject);
			addEventListener(MouseEvent.MOUSE_OUT, removeObjectHighlight);
		}
		
		public function highlightObject(e:MouseEvent):void {		
			var X = e.localX
			var Y = e.localY;
			objectHighlight = new Shape( ); 
            objectHighlight.graphics.beginFill( 0xff9933 , 1 );
            objectHighlight.graphics.drawCircle(X, Y, 40)
			addChildAt(objectHighlight, 0);			
		}
		
		public function removeObjectHighlight(e:MouseEvent):void {
			removeChild(objectHighlight);
		}
		
		public function updateLines(e:MouseEvent):void {
			for (var i:int = 0; i < connectedObjects.length; i++) {
				redrawLine(connectedObjects[i][0], connectedObjects[i][1], connectedObjects[i][2]);
			}
		}		
		
		public function startMove(e:MouseEvent):void {
			//highlight
			//highlightObject(e)
			startDrag();
			addEventListener(MouseEvent.MOUSE_MOVE, updateLines);
		}
		
		public function stopMove(e:MouseEvent):void {		
			//remove highlight
			//removeObjectHighlight(e)
			stopDrag();
			removeEventListener(MouseEvent.MOUSE_MOVE, updateLines);
		}
		
		public function clickDrawLine(e:MouseEvent):void {
			clickedObjects.length %= 2;
			clickedObjects.push(e.currentTarget);
			
			// check with the Game class for permission to draw lines...
			
			drawLines();
		}
				
		// static vars & arrays
		//
		static var clickedObjects:Array = new Array();
		static var connectedObjects:Array = new Array();
		
		public function updateConnectedObjects(object1:Host, object2:Host, line:Shape) {
			connectedObjects.push([object1, object2, line]);
			trace("add - " + connectedObjects.length + " - " + connectedObjects);
		}
		
		public function updateRemoveConnectedObjects(object1:Host, object2:Host) {
			for (var i1:int = 0; i1 < connectedObjects.length; i1++) {
				if(object1.hostId == connectedObjects[i1][0].hostId && object2.hostId == connectedObjects[i1][1].hostId ||
					object2.hostId == connectedObjects[i1][0].hostId && object1.hostId == connectedObjects[i1][1].hostId) {
						var line2 = connectedObjects[i1][2];
						line2.graphics.clear();
						if (i1 == 0) {
							connectedObjects.pop();
						} else { 
							connectedObjects.splice(i1, i1); 
						}
				}
			}
			trace("remove - " + connectedObjects.length + " - " + connectedObjects);
		}
				
		public function drawLines() {
			var line:Connection = new Connection();
			
			line.graphics.clear();
			line.graphics.lineStyle(6, 000000);
	
			var len:int = clickedObjects.length - 1;
	
			for(var i:int = 0; i < len; i++){
				var c1 = clickedObjects[i];
				var c2 = clickedObjects[i + 1];				
				
				//check with Game if the objects are Host and are already connected 
				//
				if ( (c1 is Host && c2 is Host) && (c1 != c2) ) {
					if ( Game.connectionExists(c1, c2) == false ) {
						line.graphics.moveTo(c1.x, c1.y);
						line.graphics.lineTo(c2.x, c2.y);
						// add line
						stage.addChildAt(line, 0);
						// update connection tables
						updateConnectedObjects(c1, c2, line);
						Game.updateConnectedObjectsIDs(c1, c2);
						
					} else {
						// if a line exists, it will be removed
						// bug spotted!, not working, will use the reset button for the moment
						//Game.updateRemoveConnectedObjectsIDs(c1, c2);
						//updateRemoveConnectedObjects(c1, c2);
					}
				}
			}
		}
		
		public function redrawLine(h1:Host, h2:Host, line:Shape):void {
            line.graphics.clear();
            line.graphics.lineStyle(3, 0, 1);
            line.graphics.moveTo(h1.x, h1.y);
            line.graphics.lineTo(h2.x, h2.y);
        }
		
		// clear all
		public function clearAll():void {
			for (var i3:int = 0; i3 < connectedObjects.length; i3++) {	
				connectedObjects[i3][2].graphics.clear();
			}
			connectedObjects.splice(0);
			connectedObjects.pop();
		}
   }
}
